package com.boarsoft.message.activemq;

import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

public class AmqProducer implements AmqMsgStub {
	public static final short TYPE_QUEUE = 0;

	protected int deliveryMode = DeliveryMode.NON_PERSISTENT;
	/** 0~9 */
	protected int priority;
	protected long timeToLive;
	protected int sendTimeout;

	/** */
	protected MessageProducer producer;

	/** */
	protected Session session;

	public void close() throws JMSException {
		producer.close();
		producer = null;
		session = null;
	}

	@Override
	public void init(Session session, Destination destination) throws JMSException {
		this.session = session;
		producer = session.createProducer(destination);
		producer.setDeliveryMode(deliveryMode);
		// producer.setDisableMessageID(value);
		// producer.setDisableMessageTimestamp(value);
		producer.setPriority(priority);
		producer.setTimeToLive(timeToLive);
	}

	public void send(Message msg) throws Exception {
		producer.send(msg);
	}

	/**
	 * 提供给AmqDesitination调用，以在断线后重设session
	 * 
	 * @param session
	 */
	public void setSession(Session session) {
		this.session = session;
	}

	public MessageProducer getProducer() {
		return producer;
	}

	public int getDeliveryMode() {
		return deliveryMode;
	}

	public void setDeliveryMode(int deliveryMode) {
		this.deliveryMode = deliveryMode;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public long getTimeToLive() {
		return timeToLive;
	}

	public void setTimeToLive(long timeToLive) {
		this.timeToLive = timeToLive;
	}

	public int getSendTimeout() {
		return sendTimeout;
	}

	public void setSendTimeout(int sendTimeout) {
		this.sendTimeout = sendTimeout;
	}
}
